using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Xml;
using Northwoods.Go;
using Northwoods.Go.Xml;

namespace Workflows.Diagrams
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ProcessDocument : GoDocument
    {
        #region members
        //public event UpdateProperty UpdatePropertyEvent;
        //public event UpdateTitle UpdateTitleEvent;
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public ProcessDocument()
        { }

        #region overrides 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override RectangleF GetAvoidableRectangle(GoObject obj)
        {
            RectangleF rect = obj.SelectionObject.Bounds;
            return rect;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        public void SetLinkPen(Pen p)
        {
            this.SkipsUndoManager = true;
            foreach (GoObject obj in this.LinksLayer)
            {
                FlowLink link = null;
                if (obj.GetType() == typeof(FlowLink))
                    link = (FlowLink)obj;
                if (link != null)
                    link.Pen = p;
            }
            this.SkipsUndoManager = false;
        }

        #region persistence
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fs"></param>
        public void Store(System.IO.Stream fs)
        {
            GoXmlWriter writer = new GoXmlWriter();
            writer.RootElementName = "WorkflowDesigner";
            writer.AddTransformer(new TransformActivityNode());
            writer.AddTransformer(new TransformActivityPort());
            writer.AddTransformer(new TransformFlowLink());
            writer.AddTransformer(new TransformComment());
            writer.Objects = this;
            writer.Generate(fs);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fs"></param>
        public void Load(System.IO.Stream fs)
        {
            this.StartTransaction();
            this.Clear();
            GoXmlReader reader = new GoXmlReader();
            reader.AddTransformer(new TransformActivityNode());
            reader.AddTransformer(new TransformActivityPort());
            reader.AddTransformer(new TransformComment());
            reader.AddTransformer(new TransformFlowLink());
            this.FinishTransaction("Loaded workflow design from file");
        }
        #endregion
    }
}
